<?php
	namespace app\index\controller;

	use \think\Db;

	class Register extends Base{

		public function index(){

			try{

				if($this->method=='post'){

					$user=input('post.user');

					if($user==''){

						return $this->fail('请输入用户名~');

					}

					$pwd=input('post.pwd');

					if($pwd==''){

						return $this->fail('请输入密码~');

					}

					$re_pwd=input('post.re_pwd');

					if($pwd!=$re_pwd){

						return $this->fail('两次密码输入不一致~');

					}

					$tel=input('post.tel');

					if($tel==''){

						return $this->fail('请输入手机号~');

					}

					// "u_id" : 1, "u_user" : "user123", "u_pwd" : "efd398f9c21a334f1c3940de1862d5e8", "u_icon" : "", "u_tel" : "15367252852", "u_money" : "100" 

					$row=Db::name('user')->whereOr(['u_user'=>$user,'u_tel'=>$tel])->find();

					if($row){

						if($row['u_user']==$user){

							return $this->fail('该用户已存在~');

						}

						if($row['u_tel']==$tel){
							return $this->fail('该手机号已被注册~');
						}



					}

					$primary=Db::name('primary')->where(['type'=>'user'])->find();

					if($primary){

						$data['u_id']=$primary['id']+1;

						$res=Db::name('primary')->where(['type'=>'user'])->setInc('id',1);

					}else{
						$data['u_id']=1;

						$res=Db::name('primary')->insert(['type'=>'user','id'=>1]);
					}

					$data['u_user']=$user;

					$data['u_pwd']=md5($pwd);

					$data['u_icon']='';

					$data['u_tel']=$tel;

					$data['u_money']=0;

					$res2=Db::name('user')->insert($data);

					if($res2){
						return $this->success();
					}

					return $this->fail('注册失败');

				}else{

					echo '拒绝访问';

				}

			}catch(\Exception $e){

				return $this->err($e);

			}


		}

	}
?>